package gui.launch;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import data.Player;
import data.Type;

public class PlayerListPanel extends JPanel implements ActionListener {
	private static final long	serialVersionUID	= -765643674525561975L;
	private JButton				openButton;
	private JFileChooser		fileChooser;
	private Vector<Player>		players;
	private static final String defaultPlayersFile = "player.dat";

	public PlayerListPanel() {
		super();

		this.players = loadPlayers(new File(defaultPlayersFile));
		build();
	}

	private Vector<Player> loadPlayers(File f) {
		Vector<Player> players = new Vector<Player>();

		try {
			FileInputStream fis = new FileInputStream(f);
			ObjectInputStream ois = new ObjectInputStream(fis);

			Player p = (Player) ois.readObject();
			while (p != null) {
				players.add(p);
				p = (Player) ois.readObject();
			}
			
			ois.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		if (players.size() == 0) {
			players.add(new Player("Laurent", 1850, Type.HUMAN));
			players.add(new Player("Chessozor", 1499, Type.COMPUTER));
		}

		return players;

	}

	private void savePlayers() {
		try {
			FileOutputStream fos = new FileOutputStream(defaultPlayersFile);
			ObjectOutputStream oos = new ObjectOutputStream(fos);

			for(Player p : players) {
				oos.writeObject(p);
			}
			
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	private void build() {
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints(0, 0, 1, 1, 1, 1,
				GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(
						0, 0, 0, 0), 0, 0);

		openButton = new JButton("Open");
		openButton.addActionListener(this);
		add(openButton, c);

		fileChooser = new JFileChooser(System.getProperty("user.dir"));
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setAcceptAllFileFilterUsed(false);
		fileChooser.setFileFilter(new PlayerFileFilter());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		if (source == openButton) {
			int returnVal = fileChooser.showOpenDialog(this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
			}
		}
	}
}
